//
//  WSRegistSViewController.m
//  GroupFourFM
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 GroupFour. All rights reserved.
//

#import "WSRegistSViewController.h"
#import "WSRegistSView.h"
#import <BmobSDK/Bmob.h>
#import <WSProgressHUD.h>

@interface WSRegistSViewController ()<WSRegistSViewDelegate>

@end

@implementation WSRegistSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    WSRegistSView *registS = [[WSRegistSView alloc] init];
    [self.view addSubview:registS];
    registS.delegate = self;
    [registS mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
}

- (void)backViewController {

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getTestWord {

    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:_phoneNum andTemplate:@"test" resultBlock:^(int number, NSError *error) {
        if (error) {
            
            NSLog(@"%@",error);
        }
    }];
}

- (void)registWithPassWord:(NSString *)passWord repassWord:(NSString *)repassWord testWord:(NSString *)testWord {

    if (passWord.length >= 6 && repassWord.length >= 6) {
        
        NSString* number=@"^[a-zA-Z0-9]\\w{5,15}$";
        NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
        if ([numberPre evaluateWithObject:passWord]) {
            
            if ([passWord isEqualToString:repassWord]) {
                BmobUser *buser = [[BmobUser alloc] init];
                buser.mobilePhoneNumber = _phoneNum;
                buser.password = passWord;
                [buser signUpOrLoginInbackgroundWithSMSCode:testWord block:^(BOOL isSuccessful, NSError *error) {
                    if (error) {
                        NSLog(@"%@",error);
                        [WSProgressHUD showImage:nil status:@"验证码错误"];
                    } else {
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }];
            } else {

                [WSProgressHUD showImage:nil status:@"密码不一致"];
            }
            
        }
        else {
            
            [WSProgressHUD showImage:nil status:@"格式不正确或输入非法字符"];
        }
    }
    else {
        
        [WSProgressHUD showImage:nil status:@"密码长度大于6"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
